<?php
namespace Home\Controller;
use Think\Controller;
class GoodsOrderController extends Controller {
    public function addorder($buyerid,$igoodsid){
    	$order=new \Model\GoodsOrderModel();	
		$goods=new \Model\IntegralGoodsModel();	
    	$goodsinfo=D('integralgoods')->find($igoodsid);
		$buyer=D('user')->find($buyerid);
    	if($goodsinfo['count']>0){//判断数量是否够
    		$orderinfo=array('igoodsid'=>$goodsinfo['id'],
    		'igoodsname'=>$goodsinfo['name'],
			'buyerid'=>$buyer['id'],
			'buyername'=>$buyer['name'],
			'count'=>1,
			'buytime'=>date("Y-m-d H:i:s"));
			//echo $sql;
    		if($order->add($orderinfo)){//添加成功
				$sql="update integralgoods set count=count-1,salecount=salecount+1 where id=$igoodsid";
				$goods->execute($sql);
				$this->success('Index/success');
				$this->redirect('IntegralGoods/goalstore',array(),3," ");
			}else{
				$this->error('Index/error');
				$this->redirect('IntegralGoods/goalstore',array(),3," ");
			}
		}else{
			//$this->error('Index/error');
			echo "<script>alert('商品库存不足!')</script>";
		}
		
	}
	
}